package com.yl.googleplayer_yl.fragment;

import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.ListView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.yl.googleplayer_yl.R;
import com.yl.googleplayer_yl.adapter.HomeAdapter;
import com.yl.googleplayer_yl.adapter.HomePagerAdapter;
import com.yl.googleplayer_yl.bean.AppInfo;
import com.yl.googleplayer_yl.bean.Home;
import com.yl.googleplayer_yl.utils.ApiService;
import com.yl.googleplayer_yl.utils.Retrofit2Util;

import java.util.ArrayList;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2016/10/30 0030.
 */
public class HomeFragment extends BaseFragment {

    private static final String TAG = "HomeFragment";
    private Home body;
    private PullToRefreshListView ptr_listview;
    public ArrayList<AppInfo> list = new ArrayList<>();
    private ListView mListView;
    private HomeAdapter homeAdapter;
    private ViewPager viewPager;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            int currentItem = viewPager.getCurrentItem();
            currentItem++;
            if (currentItem>picture.size()-1) {
                currentItem = -1;
            }
            viewPager.setCurrentItem(currentItem,true);
            handler.sendEmptyMessageDelayed(0, 2500);

        }
    };
    private ArrayList<String> picture;

    @Override
    protected View getSuccessView() {
        initPtr();
        requestData();

        return ptr_listview;
    }



    @Override
    protected Object requestData() {
        checkPullFromStart();
        ApiService apiService = Retrofit2Util.getRetrofit(ApiService.SERVER_HOST).create(ApiService.class);
        apiService.getAppInfoList(""+list.size()).enqueue(new Callback<Home>() {
            @Override
            public void onResponse(Call<Home> call, Response<Home> response) {
                body = response.body();
                if (body != null) {
                    //设置头布局数据

                    picture = body.getPicture();
                    viewPager.setAdapter(new HomePagerAdapter(picture));
                    list.addAll(body.getList());

                    if (list != null) {

                        homeAdapter = new HomeAdapter(list);
                        mListView.setAdapter(homeAdapter);
                    }
                    homeAdapter.notifyDataSetChanged();
                    ptr_listview.onRefreshComplete();
                    int i = list.size() - body.getList().size();
                    if (i > 0) {
                        mListView.setSelection(i-4);
                    } else {
                        mListView.setSelection(i);
                    }

                    Log.e(TAG, "onResponse: " + list.get(0).getName());
                } else {
                    Log.e(TAG, "onResponse: 请求数据为空");
                }
            }
            @Override
            public void onFailure(Call<Home> call, Throwable t) {
                t.printStackTrace();

            }
        });
        return body;
    }

    /**
     * 检查如果是下拉刷新就清空集合
     */
    private void checkPullFromStart() {
        if (ptr_listview.getCurrentMode()== PullToRefreshBase.Mode.PULL_FROM_START) {
            list.clear();
        }
    }

    private void initPtr() {
        ptr_listview = (PullToRefreshListView) View.inflate(getActivity(), R.layout.ptr_listview, null);

        //1.设置刷新模式
        ptr_listview.setMode(PullToRefreshBase.Mode.BOTH);//设置既可以上拉也可以下拉
        //2.设置刷新监听器
        ptr_listview.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            //下拉刷新和上拉加载更多都会执行该方法，
            // @param refreshView
            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                //直接调用
                loadPage.loadDataAndRefreshPage();
            }
        });
        mListView = ptr_listview.getRefreshableView();
        mListView.setDividerHeight(0);//去掉分割线
        mListView.setSelector(android.R.color.transparent);//将listveiw的selector设置为透明
        addHeaderView();
    }

    private void addHeaderView() {
        View headerView = View.inflate(getActivity(), R.layout.layout_home_header,null);
        viewPager = (ViewPager) headerView.findViewById(R.id.viewPager);
        mListView.addHeaderView(headerView);
    }

    @Override
    public void onStart() {
        super.onStart();
        handler.sendEmptyMessageDelayed(0, 2500);
    }

    @Override
    public void onStop() {
        super.onStop();
        Log.e(TAG, "onStop: 移除homeFragment" );
        handler.removeMessages(0);
    }
}
